package com.ruoyi.project.cyq.statistics.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.cyq.order.domain.Order;
import com.ruoyi.project.cyq.statistics.domain.IOrderGenre;
import com.ruoyi.project.cyq.statistics.service.OrderGenreService;
import com.ruoyi.project.system.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘鹿
 * @date 2020-09-18 15:53
 */
@Controller
@RequestMapping("/cyq/statistics")
public class OrderGenreController extends BaseController {

    private String prefix = "cyq/statistics";

    @Autowired
    private OrderGenreService orderGenreService;

    //跳转到页面
    @RequiresPermissions("cyq:statistics:view")
    @GetMapping()
    public String operlog() {
        return prefix + "/statistics";
    }

    //查询数据
    @RequiresPermissions("cyq:statistics:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IOrderGenre iOrderGenre) {
        startPage();
        List<IOrderGenre> list = orderGenreService.selectGenreList(iOrderGenre);
        return getDataTable(list);
    }

    //跳转到添加页面
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    //添加订单类别
    @RequiresPermissions("cyq:statistics:add")
    @Log(title = "订单类别名称", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated IOrderGenre iOrderGenre) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(orderGenreService.checkGenreNameUnique(iOrderGenre))) {
            return error("新增类别'" + iOrderGenre.getGenreName() + "'失败，类别名称已存在");
        }
        return toAjax(orderGenreService.insertOrderGenre(iOrderGenre));
    }

    //修改数据
    @GetMapping("/edit/{genreId}")
    public String edit(@PathVariable("genreId") Long genreId, ModelMap mmap) {
        //通过ID查询数据封装
        mmap.put("OrderGenre", orderGenreService.selectGenreById(genreId));
        return prefix + "/edit";
    }

    //修改类别
    @RequiresPermissions("cyq:statistics:edit")
    @Log(title = "订单类别管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated IOrderGenre iOrderGenre) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(orderGenreService.checkGenreNameUnique(iOrderGenre))) {
            return error("修改类别'" + iOrderGenre.getGenreName() + "'失败，类别名称已存在");
        }
        return toAjax(orderGenreService.updateOrderGenre(iOrderGenre));
    }

    //删除类别
    @RequiresPermissions("cyq:statistics:remove")
    @Log(title = "订单类别管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(long[] ids) {
        System.out.println(ids);
        return toAjax(orderGenreService.deleteOrderGenreByIds(ids));
    }
}
